<template>
  <div class="WP">
    <div id="lthxt"></div>
    <div class="mark"></div>
  </div>
</template>

<script>
import { defineComponent, ref, computed, nextTick } from "vue";
export default defineComponent({
  name: "Lthxt",
});
</script>
<script setup>
import * as echarts from "echarts";
import "echarts-gl";
import imageBase64Data from "../pmhxt/base64.json";

// data
const lthxtData = ref([
  {
    name: "水质异常",
    value: 180,
    itemStyle: {
      color: "rgba(255, 146, 16, 1)",
    },
  },
  {
    name: "正常点位",
    value: 250,
    itemStyle: {
      color: "rgba(0, 121, 221, 1)",
    },
  },
  {
    name: "风险源",
    value: 150,
    itemStyle: {
      color: "rgba(232, 32, 14, 1)",
    },
  },
]);

// 使用3d饼图需要引入echarts-gl    import "echarts-gl";
function getParametricEquation(
  startRatio,
  endRatio,
  isSelected,
  isHovered,
  k,
  h
) {
  // 计算
  let midRatio = (startRatio + endRatio) / 2;

  let startRadian = startRatio * Math.PI * 2;
  let endRadian = endRatio * Math.PI * 2;
  let midRadian = midRatio * Math.PI * 2;
  isSelected = false;
  k = typeof k !== "undefined" ? k : 1 / 3;
  let offsetX = isSelected ? Math.sin(midRadian) * 0.1 : 0;
  let offsetY = isSelected ? Math.cos(midRadian) * 0.1 : 0;
  let hoverRate = isHovered ? 1.05 : 1;
  return {
    u: {
      min: -Math.PI,
      max: Math.PI * 3,
      step: Math.PI / 32,
    },

    v: {
      min: 0,
      max: Math.PI * 2,
      step: Math.PI / 20,
    },

    x: function (u, v) {
      if (u < startRadian) {
        return (
          offsetX + Math.cos(startRadian) * (1 + Math.cos(v) * k) * hoverRate
        );
      }
      if (u > endRadian) {
        return (
          offsetX + Math.cos(endRadian) * (1 + Math.cos(v) * k) * hoverRate
        );
      }
      return offsetX + Math.cos(u) * (1 + Math.cos(v) * k) * hoverRate;
    },

    y: function (u, v) {
      if (u < startRadian) {
        return (
          offsetY + Math.sin(startRadian) * (1 + Math.cos(v) * k) * hoverRate
        );
      }
      if (u > endRadian) {
        return (
          offsetY + Math.sin(endRadian) * (1 + Math.cos(v) * k) * hoverRate
        );
      }
      return offsetY + Math.sin(u) * (1 + Math.cos(v) * k) * hoverRate;
    },

    z: function (u, v) {
      if (u < -Math.PI * 0.5) {
        return Math.sin(u);
      }
      if (u > Math.PI * 2.5) {
        return Math.sin(u) * h * 0.1;
      }
      return Math.sin(v) > 0 ? 1 * h * 0.1 : -1;
    },
  };
}

function getPie3D(pieData, internalDiameterRatio) {
  let series = [];
  let sumValue = 0;
  let startValue = 0;
  let endValue = 0;
  let legendData = [];
  let k =
    typeof internalDiameterRatio !== "undefined"
      ? (1 - internalDiameterRatio) / (1 + internalDiameterRatio)
      : 1 / 3;
  for (let i = 0; i < pieData.length; i++) {
    sumValue += pieData[i].value;

    let seriesItem = {
      name:
        typeof pieData[i].name === "undefined" ? `series${i}` : pieData[i].name,
      type: "surface",
      parametric: true,
      wireframe: {
        show: false,
      },
      roa: false,
      pieData: pieData[i],
      startAngle: 250,
      pieStatus: {
        selected: false,
        hovered: false,
        k: 1 / 10,
      },
    };
    if (typeof pieData[i].itemStyle != "undefined") {
      let itemStyle = {};
      typeof pieData[i].itemStyle.color != "undefined"
        ? (itemStyle.color = pieData[i].itemStyle.color)
        : null;
      typeof pieData[i].itemStyle.opacity != "undefined"
        ? (itemStyle.opacity = pieData[i].itemStyle.opacity)
        : null;

      seriesItem.itemStyle = itemStyle;
      seriesItem.roam = false;
    }
    series.push(seriesItem);
  }
  for (let i = 0; i < series.length; i++) {
    endValue = startValue + series[i].pieData.value;

    series[i].pieData.startRatio = startValue / sumValue;
    series[i].pieData.endRatio = endValue / sumValue;
    series[i].parametricEquation = getParametricEquation(
      series[i].pieData.startRatio,
      series[i].pieData.endRatio,
      false,
      false,
      k,
      series[i].pieData.value
    );

    startValue = endValue;

    legendData.push(series[i].name);
  }

  let imgBase64Url = imageBase64Data.bottomBgUrl;
  let option = {
    roam: false,
    disableMouseEvent: true,
    graphic: [
      {
        type: "image",
        z: 0,
        style: {
          // padding: [50, 50],
          image: imgBase64Url,
          width: 180,
          height: 100,
        },
        left: "center",
        top: 110,
      },
    ],
    backgroundColor: "transparent",
    legend: {
      textStyle: {
        color: "#FFFFFF",
        fontSize: 12,
      },
      data: legendData,
      formatter: (params) => {
        return params;
      },
    },
    xAxis3D: {},
    yAxis3D: {},
    zAxis3D: {},
    grid3D: {
      viewControl: {
        autoRotate: false,
      },
      left: "center",
      width: "50%",
      show: false,
      boxHeight: 20,
    },
    series: series,
    dataZoom: [],
  };
  return option;
}

nextTick(() => {
  // devicePixelRatio: 设备像素比，默认取浏览器的值window.devicePixelRatio
  let myChart = echarts.init(document.getElementById("lthxt"));
  // 传入数据生成 option
  let option = getPie3D(lthxtData.value, 0.65);
  // myChart.setStatus({
  //   dragEnable: false, // 地图是否可通过鼠标拖拽平移，默认为true
  //   zoomEnable: true, //地图是否可缩放，默认值为true
  // });
  myChart.setOption(option);
  // 添加事件
  // bindListen(myChart, option, 10);
});
</script>

<style scoped>
.WP {
  position: relative;
  width: 30vw;
  height: 30vh;
}

#lthxt {
  width: 30vw;
  height: 30vh;
  background: black;
}

.mark {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 30vw;
  height: 25vh;
  /* background: blue; */
}
</style>
